import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import HomeView from "@/views/HomeView.vue"
import UsersView from "@/views/user/UsersView.vue"
import IndexView from "@/views/IndexView.vue"
import AuthorityList from '@/views/authoritymanage/AuthorityList.vue';
import RolesList from '@/views/authoritymanage/RolesList.vue';
import GoodList from '@/views/goodmanage/GoodList.vue';
import GoodAdd from '@/views/goodmanage/GoodAdd.vue';
import GoodCategories from '@/views/goodmanage/GoodCategories.vue';
import GoodParams from '@/views/goodmanage/GoodParams.vue';
import OrdersView from '@/views/ordermanage/OrdersView.vue';
import WelcomeView from '@/views/home/WelcomeView.vue';
const routes = [
  {
    path: '/',
    name: 'index',
    component: IndexView
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    // 转发,改变请求的地址
    redirect: "/home/welcome",
    name: 'home',
    component: HomeView,
    children: [
      // 用户列表
      {
        path: 'welcome',
        name: 'welcome',
        component: WelcomeView
      },
      // 用户列表
      {
        path: 'users',
        name: 'users',
        component: UsersView
      },
      // 权限列表
      {
        path: 'rights',
        name: 'rights',
        component: AuthorityList
      },
      // 角色列表
      {
        path: 'roles',
        name: 'roles',
        component: RolesList
      },
      // 商品列表
      {
        path: 'goods',
        name: 'goods',
        component: GoodList
      },
      // 分类参数
      {
        path: 'params',
        name: 'params',
        component: GoodParams
      },
      // 商品列表
      {
        path: 'goods/add',
        name: 'goodsadd',
        component: GoodAdd
      },
      // 分类参数
      {
        path: 'categories',
        name: 'categories',
        component: GoodCategories
      },
      // 订单管理
      {
        path: 'orders',
        name: 'orders',
        component: OrdersView
      }
    ]
  }

]
// 使用路由
Vue.use(VueRouter)
const router = new VueRouter({
  // 路由不使用#
  mode: "history",
  routes
})
// 路由守卫,必须先登录,再访问后台
router.beforeEach((to, from, next) => {
  // ... to 要去的路径对象, from从哪个路径对象来
  // console.log(to, from);
  // 去login
  if (to.path === "/login" || to.path === "/") {
    // 放行,可以访问
    next()
    // 想去home 必须验证token
  } else {
    const token = sessionStorage.getItem("token")
    // 如果非空的放行   // 字符串不是" 就是true
    if (token) {
      next()
    } else {
      next("/login")
    }
  }
})
export default router
